package com.example.programming.sys.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.example.programming.common.Result;
import com.example.programming.sys.mapper.EduhomeworkMapper;
import com.example.programming.sys.entity.Eduhomework;

import com.example.programming.common.tools.BaseController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-09-16
 */
@RestController
@RequestMapping("/sys/eduhomework")
@CrossOrigin
public class EduhomeworkController extends BaseController {
    @Autowired
    private EduhomeworkMapper eduhomeworkMapper;

    @ApiImplicitParams({
    @ApiImplicitParam(name = "pageNum",paramType = "query",value = "当前页码",required = true),
    @ApiImplicitParam(name = "pageSize",paramType = "query",value = "每页显示记录数",required = true)
    })
    @ApiOperation(value = "分页获取Eduhomework信息")
    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<Eduhomework> page = new Page<Eduhomework>(pageNum, pageSize);
        Eduhomework parms = new Eduhomework();
        QueryWrapper<Eduhomework> warpper = new QueryWrapper<Eduhomework>(parms);

        result.setData(eduhomeworkMapper.selectPage(page, warpper));
        return result;
    }
    @ApiOperation(value = "保存修改Eduhomework信息")
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Eduhomework entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            eduhomeworkMapper.insert(entity);
        } else {
            eduhomeworkMapper.updateById(entity);
        }
        return result;
    }
    @ApiOperation(value = "按id删除Eduhomework，可以传入多个id用，隔开")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "ids",paramType = "query",value = "传入的id串，用，隔开",required = true)
    })
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        eduhomeworkMapper.deleteBatchIds(deleteIds);
        return result;
    }

    @GetMapping("getwork/{vid}")
    public Result getwork(@PathVariable("vid") String vid) {
        Result result = new Result();
        QueryWrapper<Eduhomework> wrapper = new QueryWrapper<>();
        wrapper.eq("vid", vid);
        result.setData(eduhomeworkMapper.selectOne(wrapper));
        return result;
    }

    @PostMapping("uploadwork")
    public String uploadwork(MultipartFile file) {
        Result result = new Result();
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        //云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI4GEj4aBvnfG3EZU71UwK";
        String accessKeySecret = "O0FFTyzQelUX7gVK3k5nxEpceZxmNE";
        String bucketName = "yu-edu";

        //创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        //上传文件流。
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            String filename = file.getOriginalFilename();
            String s = UUID.randomUUID().toString().replaceAll("-", "");
            filename = s + filename;
            String s1 = new DateTime().toString("yyyy/MM/dd");
            filename = s1 + "/" + filename;
            ossClient.putObject(bucketName, filename, inputStream);
            //关闭OSSClient。
            ossClient.shutdown();

            String url = "https://" + bucketName + "." + endpoint + "/" + filename;
            return url;
        }catch (Exception e) {
            return null;
        }

    }
}
